function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";path=/;expires="+exdate.toGMTString());
}

function d2h(d) {
    d = parseInt(d);
    s = d.toString(16);
    if (s.length == 1) return '0' + s;
    return s;
}

var current_color = ""
 
var age_black = "Persistent / New"
var contro_black = "Consistency / Controversy"
var grey_black = "Added / Removed"

var age_selected = "<span style='background-color:#DDFFFF;'>Persistent</span> / <span style='background-color:#FFFFDD;'>New</span>"
var contro_selected = "<span style='background-color:#DDEEFF;'>Consistency</span> / <span style='background-color:#FFEEDD;'>Controversy</span></label>"
var grey_selected = "<span style='background-color:#DDFFDD;'>Added</span> / <span style='background-color:#FFDDDD;'>Removed</span>"

function switchColor(new_color) {
    if (current_color == new_color) return;    
    current_color = new_color;
    setCookie('color', new_color, 365);

    generateContent();
}

function generateContent() {
    if (current_color == "") {
        // set revision count
        elem = document.getElementById('revision')
        if (elem != null) elem.innerHTML = revisions + ' revisions.';
        
        // get the cookie
        current_color = 'age';
        c = getCookie('color')
        if (c != null && c !="") current_color = c;
     }
        
    // check the selected radio
    var label1 = age_black
    var label2 = contro_black
    var label3 = grey_black
    if (current_color == 'age') {
        label1 = age_selected;
        document.forms[0].age.checked = true;
    }
    else if (current_color == 'contro') {
        label2 = contro_selected;
        document.forms[0].contro.checked = true;
    }  
    else if (current_color == 'grey') {
        label3 = grey_selected;
        document.forms[0].grey.checked = true;
    }
    document.getElementById('agelabel').innerHTML = label1;
    document.getElementById('controlabel').innerHTML = label2;
    document.getElementById('greylabel').innerHTML = label3;
    
    var content = ""
    var j = 0;
    for (i=0; i < j_word.length; i++) {
    
        word = j_word[i];
        
        if (word[0] == '<') {
            content += word + ' ';
            continue;
        }   
        
        add = parseFloat(j_add_conf[j]);
        old = parseFloat(j_old_conf[j]);
        non = parseFloat(j_non_conf[j]);
        j += 1
        
        if (current_color == "grey") {
            
            r = 255
            g = 255
            b = 255
            
            neg = 1 - add;
            pos = add;
            
            if (neg > pos) {
                g = 255 - (neg-pos) * 50;
                b = g;
            } else {
                r = 255 - (pos-neg) * 30;
                b = r;
            };
            
        } else if (current_color == "age") {
            r = 255
            g = 255
            b = 255
            
            neg = 1 - old;
            pos = old;
            
            if (neg > pos) {
                b = 255 - (neg-pos) * 50;
                //r = g;
            } else {
                r = 255 - (pos-neg) * 30;
                //b = r;
            }
            
        
        } else if (current_color == "contro") {
            r = 255
            g = 255
            b = 255
            
            neg = 1 - non;
            pos = non;
            
            if (neg > pos) {
                b = 255 - (neg-pos) * 30;
                g = 255 - (neg-pos) * 15;
            } else {
                r = 255 - (pos-neg) * 30;
                g = 255 - (pos-neg) * 15;
            }
            
            
        }
        
        color = d2h(r) + d2h(g) + d2h(b);
        highlight = "<span style=\"background-color:#" + color + ";\">";    
        content += highlight + word + "</span>";
        
    }
    if (content == '') content = '<p>&nbsp;</p>';
    document.getElementById("articleContent").innerHTML = content;
}
